package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.inter.OnRVItemClickListener;
import com.wangtao.firstapp.inter.OnRVItemLongClickListener;
import com.wangtao.firstapp.modles.MyAttentionData;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 我的关注页面的适配器
 * Created by lanouhn on 16/9/21.
 */
public class MyAttentionAdapter extends RecyclerView.Adapter<MyAttentionAdapter.ViewHolder> {
    private Context context;
    private MyAttentionData myAttentionData;
    private OnRVItemClickListener clickListener;
    private OnRVItemLongClickListener longClickListener;

    public void setClickListener(OnRVItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setLongClickListener(OnRVItemLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    public MyAttentionAdapter(Context context, MyAttentionData myAttentionData) {
        this.context = context;
        this.myAttentionData = myAttentionData;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_recyclerview_myattention, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Picasso.with(context).load(myAttentionData.getData().getList().get(position).getAvatar()).config(Bitmap.Config.RGB_565).into(holder.cir_header_myattention);
        holder.tv_name_myattention.setText(myAttentionData.getData().getList().get(position).getNickname());

        if (clickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int clickposition = holder.getLayoutPosition();
                    clickListener.OnRVItemClickListener(clickposition);
                }
            });
        }
        if (longClickListener != null) {
            holder.itemView.setOnTouchListener(new View.OnTouchListener() {


                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        int clickposition = holder.getLayoutPosition();
                        longClickListener.OnRVItemLongClickListener(clickposition);
                    }
                    return true;
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return myAttentionData.getData().getList().size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        private CircleImageView cir_header_myattention;// 我关注的人的头像
        private TextView tv_name_myattention;// 我关注的人的名字

        public ViewHolder(View itemView) {
            super(itemView);
            cir_header_myattention = (CircleImageView) itemView.findViewById(R.id.cir_header_myattention);
            tv_name_myattention = (TextView) itemView.findViewById(R.id.tv_name_myattention);
        }
    }
}
